package com.jiantong.web.open.app.lawEnforcing;

import com.jiantong.lawEnforcing.model.Evidences;
import com.jiantong.lawEnforcing.service.EvidencesService;
import com.jiantong.web.core.enums.ExceptionCode;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * 证物信息
 * Created by mz03 on 2017/8/15.
 */
@Slf4j
@OpenBean
public class EvidencesApis {
    private EvidencesService evidencesService;

    /**
     * 根据id查询一条信息
     * @param id
     * @return
     */
    @OpenMethod(
            key = "find.Evidences.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public Evidences findById(Long id){
        Response<Evidences> response = evidencesService.finById(id);
        if (!response.isSuccess()){
            log.error("failed to find Evidences read by id = {}, cause : {}", id, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }


    /**
     * 根据ids查询一组
     * @param ids
     * @return
     */
    @OpenMethod(
            key = "find.EvidencesList.by.ids",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public List<Evidences> findByIds(List<Long> ids){
        Response<List<Evidences>> response = evidencesService.finByIds(ids);
        if (!response.isSuccess()){
            log.error("failed to find EvidencesList read by ids = {}, cause : {}", ids, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }


    /**
     * 创建一条信息
     * @param evidences
     * @return
     */
    @OpenMethod(
            key = "create.Evidences",
            paramNames = {"evidences"},
            httpMethods = RequestMethod.POST
    )
    public Long create(Evidences evidences){
        Response<Long> response = evidencesService.create(evidences);
        if(!response.isSuccess()){
            log.error("failed to create evidences read by evidences = {}, cause : {}", evidences, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    /**
     * 更新单据
     */
    @OpenMethod(
            key = "update.ask.evidences",
            paramNames = {"evidences"},
            httpMethods = RequestMethod.PUT
    )
    public Boolean update(Evidences evidences){
        if(evidences.getId() == null){
            log.error("id should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "id should be not null");
        }
        Response<Boolean> response = evidencesService.update(evidences);
        if(!response.isSuccess()){
            log.error("failed to update evidences");
            throw new OPServerException(ExceptionCode.DATA_UPDATE_FAIL.value(),
                    "failed to update evidences");
        }
        return response.getResult();
    }

}
